package de.gbeine.java.depview.graph;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import de.gbeine.java.depview.model.Element;

/**
 *
 * @author gbeine
 *
 */
final class Dependencies {

    /**
     *
     */
    private final Map<String, Set<String>> dependencies = new HashMap<String, Set<String>>();

    /**
     *
     */
    public Dependencies() {
        super();
    }

    /**
     *
     * @param pSource
     * @param pTarget
     */
    public void addDependency(final Element pSource, final Element pTarget) {
        final String source = pSource.getName();
        final String target = pTarget.getName();
        if (this.dependencies.containsKey(source)) {
             if (!this.dependencies.get(source).contains(target)) {
                 this.dependencies.get(source).add(target);
             }
        } else {
            Set<String> values = new HashSet<String>();
            values.add(target);
            this.dependencies.put(source, values);
        }
    }

    /**
     *
     * @param pSource
     * @param pTarget
     * @return
     */
    public boolean dependencyExists(final Element pSource, final Element pTarget) {
        final String source = pSource.getName();
        final String target = pTarget.getName();
        return this.dependencies.containsKey(source) && this.dependencies.get(source).contains(target);
    }

}
